var Instance = Class.create({
    initialize: function(hash, cid, year, term, key, teacher, sem) {
        this.hash = hash;
        this.cid = cid;
        this.year = parseInt(year);
        this.term = 1 - parseInt(term);
        this.key = key;
        this.teacher = teacher;
        this.sem = sem;
    }
});

var Course = Class.create({
    initialize: function(name, cid, area1, area2, comp, credits, instances, module, grade, external, suggestedSemester, instance) {
        
        this.name = name;
        this.hash = cid;
        this.area1 = area1;
        this.area2 = area2;
        this.comp = comp == "comp" ? true : false;
        this.external = (parseInt(external) ? true : false);
        this.sem = parseInt(suggestedSemester);
        this.external = (parseInt(external) ? true : false);

        this.credits = parseInt(credits);
        
        this.instances = instances;
        
        if (!instance) {
            // try to match instance as first semester a course is taken + suggested sem of course
            var bestYear = Globals.timeline.guessStudyStart()+parseInt(this.sem/2);
            // check if we got one like this

            for (instanceKey in this.instances) {
                if (!this.instance && this.instances[instanceKey].year == bestYear)
                    this.instance = this.instances[instanceKey];
            }
            // Okay, no perfect match, take next best
            if (!this.instance) {
                for (year = bestYear; !this.instance && year <= Globals.endYear; year ++) {
                    for (instanceKey in this.instances) {
                        if (!this.instance && this.instances[instanceKey].year == year)
                            this.instance = this.instances[instanceKey];
                    }
                 
                }
            }
            // no luck either, go back in time            
            if (!this.instance) {
                for (year = bestYear; !this.instance && year >= Globals.startYear; year--) {
                    for (instanceKey in this.instances) {
                        if (!this.instance && this.instances[instanceKey].year == year)
                            this.instance = this.instances[instanceKey];
                    }
                 
                }
            }
            // awww... Something went horribly wrong. take newest.
            if (!this.instance) {            
                var max = $H(this.instances).keys().max();
                this.instance = this.instances[max];
            }
            
        } else this.instance = this.instances[instance]
        
        if (grade!=undefined) this.grade = grade;
        else this.grade = 2.0;

        if (module) this.module = module;
        else this.module = (this.comp && this.area1 != '') ? this.area1 : 'comp';
        
        Globals.courses[this.hash] = (this);
        
        this.display();
    },

    update: function() {
        $('grade'+this.hash).innerHTML = grade2str(this.grade);
        $('timelinegrade'+this.hash).innerHTML = grade2str(this.grade);
        Globals.modules[this.module].update();
        updateSortables();
    },
    
    createDiv: function(tag) {
		if (!tag) tag = "";
        
        var displayName = this.name.length > 40 ?
            name.substring(0,40)+"..." : this.name;
        
        var thesisClass = (this.name == "Bachelor's Thesis") ? " thesis":"";
        
        var slider = Builder.node('div', {id:tag+'strack'+this.hash, className: 'slidetrack'}, [
            Builder.node('div', {id:tag+'grade'+this.hash, className: 'grade'}, grade2str(this.grade)),
            Builder.node('div', {id:tag+'shandle'+this.hash, className: 'slidehandle'}),
        ]);
        
        badges = [
            Builder.node('p', {className: 'handle'}, this.name),
            Builder.node('img', {src: 'bin/badge-'+this.credits+'.png'})
        ];

        if (this.area2 != '') {badges.push(Builder.node('img', {src: 'bin/badge-'+this.area2+'.png', className: "interdisciplinary"}));}
        if (this.area1 != '') {badges.push(Builder.node('img', {src: 'bin/badge-'+this.area1+'.png'}));}


        if (this.comp) {badges.push(Builder.node('img', {src: 'bin/badge-comp.png'}));}

        badges.push(slider);

        var newDiv = Builder.node('div', {id: tag+'course' + this.hash, className: 'course'+thesisClass, style:'display:none' }, badges)
        
        return newDiv;
    },
    
    activateSlider: function(tag) {
		if (!tag) tag = "";
        // create the slider object for that course
        var gradeSlide = new Control.Slider(tag+'shandle'+this.hash, tag+'strack'+this.hash, {
            range: $R(0.0,4.0),
            sliderValue: this.grade,
            axis: 'horizontal',
            alignX: -20,
            alignY: 0,
            id: this.hash,
            parent: this,
            values: [0.0, 1.0, 1.3, 1.7, 2.0, 2.3, 2.7, 3.0, 3.3, 3.7, 4.0],
            onSlide: function(v) {
                this.parent.grade = v;
                this.parent.update();
            }
        });
        
    },
    
    display: function() {
        var selfDiv = this.createDiv();
        $('mod'+this.module).appendChild(selfDiv);
        this.activateSlider();
        
		var timelineDiv = this.createDiv('timeline');
        $('timeline'+this.instance.year + this.instance.term).appendChild(timelineDiv);
        this.activateSlider('timeline');

		new Effect.Appear(timelineDiv.id, {duration: 1.0});
		new Effect.Appear(selfDiv.id, {duration: 1.0});

    },
    
    destroy: function(quick) {        
        if (quick) {
            $("timeline"+this.instance.year+this.instance.term).removeChild($("timelinecourse"+this.hash));
            $("mod"+this.module).removeChild($("course"+this.hash));
            delete Globals.courses[this.hash];
        } else {    
            new Effect.Shrink("course"+this.hash, {duration: 1});
            setTimeout('$("timeline'+this.instance.year+this.instance.term+'").removeChild($("timelinecourse'+this.hash+'")); $("mod'+this.module+'").removeChild($("course'+this.hash+'")); delete Globals.courses["'+this.hash+'"];Globals.modules["'+this.module+'"].update();', 1000);
        }

    }
});

